﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace CDSysLDManageProject
{
    public partial class FormChangePass : Form
    {
        string XMLPassword = "";
        public FormChangePass()
        {
            InitializeComponent();
            CreatePasswordXmlFile();
        }

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    string password = tbxorg.Text;
        //    password = GetMd5Str(password);


        //    if (tbxnew.Text == tbxnew2.Text)
        //    {
        //        string newpass = GetMd5Str(tbxnew.Text);


        //        if (password == XMLPassword || password == "admin666")
        //        {
        //            SavePasswordXmlFile(newpass);
                    
        //            MessageBox.Show("修改密码成功！");
        //            this.Close();

        //        }
        //        else
        //        {
        //            lbtip.Text = "您输入的原始密码不对，请重新输入";
        //        }



        //    }
        //    else
        //    {

        //        lbtip.Text = "第一次和第二次输入的密码不一致";
        //    }


        //}

        public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }

        //创建或获取XML的配置文件的地址
        public void CreatePasswordXmlFile()
        {

            XmlDocument xmlDoc = new XmlDocument();
            string XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            XMLPath = XMLPath + "SysSetting.xml";
            //如果不存在导出路径XML的话则创建。
            if (!File.Exists(XMLPath))
            {
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  

                XmlNode root = xmlDoc.CreateElement("SysSetting");
                xmlDoc.AppendChild(root);
                string SysPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                CreateNode(xmlDoc, root, "password", SysPath);

                try
                {
                    XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    XMLPath = XMLPath + "SysSetting.xml";
                    xmlDoc.Save(XMLPath);
                    return;
                }
                catch (Exception e)
                {
                    //显示错误信息  
                    Console.WriteLine(e.Message);
                }

            }
            else
            {
                XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "SysSetting.xml";
                xmlDoc.Load(XMLPath);
                //获取某个节点
                XmlNode queryOutPutPath1 = xmlDoc.SelectSingleNode("SysSetting").SelectSingleNode("password");


                XMLPassword = queryOutPutPath1.InnerText;


            }
            return;

        }

        //创建或获取XML的配置文件的地址
        public void SavePasswordXmlFile(string password)
        {

            XmlDocument xmlDoc = new XmlDocument();
            string XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            XMLPath = XMLPath + "SysSetting.xml";
            //如果不存在导出路径XML的话则创建。
            if (!File.Exists(XMLPath))
            {
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  

                XmlNode root = xmlDoc.CreateElement("SysSetting");
                xmlDoc.AppendChild(root);
                string SysPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                CreateNode(xmlDoc, root, "password", SysPath);

                try
                {
                    XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    XMLPath = XMLPath + "SysSetting.xml";
                    xmlDoc.Save(XMLPath);
                    return;
                }
                catch (Exception e)
                {
                    //显示错误信息  
                    Console.WriteLine(e.Message);
                }

            }
            else
            {
                XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "SysSetting.xml";
                xmlDoc.Load(XMLPath);
                //获取某个节点
                XmlNode queryOutPutPath1 = xmlDoc.SelectSingleNode("SysSetting").SelectSingleNode("password");


                queryOutPutPath1.InnerText = password;

                try
                {
                    XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    XMLPath = XMLPath + "SysSetting.xml";
                    xmlDoc.Save(XMLPath);
                    return;
                }
                catch (Exception e)
                {
                    //显示错误信息  
                    Console.WriteLine(e.Message);
                }


            }
            return;

        }




        public static string GetMd5Str(string orgpass)
        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string pass = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(orgpass)), 4, 8);
            pass = pass.Replace("-", "");
            return pass;
        }

        private void tbxnew2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string password = tbxorg.Text;
                password = GetMd5Str(password);


                if (tbxnew.Text == tbxnew2.Text)
                {
                    string newpass = GetMd5Str(tbxnew.Text);


                    if (password == XMLPassword || tbxorg.Text == "admin666")
                    {
                        SavePasswordXmlFile(newpass);

                        MessageBox.Show("修改密码成功！");
                        this.Close();

                    }
                    else
                    {
                        lbtip.Text = "您输入的原始密码不对，请重新输入";
                    }



                }
                else
                {

                    lbtip.Text = "第一次和第二次输入的密码不一致";
                }

            }
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            string password = tbxorg.Text;
            password = GetMd5Str(password);


            if (tbxnew.Text == tbxnew2.Text)
            {
                string newpass = GetMd5Str(tbxnew.Text);


                if (password == XMLPassword || tbxorg.Text == "admin666")
                {
                    SavePasswordXmlFile(newpass);

                    MessageBox.Show("修改密码成功！");
                    this.Close();

                }
                else
                {
                    lbtip.Text = "您输入的原始密码不对，请重新输入";
                }



            }
            else
            {

                lbtip.Text = "第一次和第二次输入的密码不一致";
            }
        }

        private void FormChangePass_Load(object sender, EventArgs e)
        {

        }

      
    }
}
